<?php

namespace App\Http\Resources\OpenShop\ShopLocation;

use App\Http\Resources\BaseResource;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPayHistoryModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Service\OpenShop\SiteLocation\PlaceHolderSiteLocationService;
use App\Service\OpenShop\Wap\SignOrderService;
use App\Service\Rms\ShopService;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class PlaceHolderSiteLocationResource extends BaseResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request)
    {
        if (empty($this->orderNo)) {
            return [];
        }

        $nearbyPlaceHolderName = OpenShopSiteLocationModel::getShopNameByIdNearByPlaceholderId($this->nearbyPlaceHolderId);

        return [
            'id'                     => $this->id,
            'source'                 => $this->source ?? 0,
            'orderNo'                => $this->orderNo ?? 0,
            'mainProcessId'          => $this->mainProcessId ?? 0,
            'longitude'              => $this->longitude ?? 0,
            'latitude'               => $this->latitude ?? 0,
            'cityArea'               => $this->cityArea ?? '',
            'provinceCode'           => $this->provinceCode ?? 0,
            'cityCode'               => $this->cityCode ?? 0,
            'areaCode'               => $this->areaCode ?? 0,
            'address'                => $this->address ?? '',
            'siteStatusText'         => OpenShopSiteLocationModel::PLACEHOLDER_SITE_STATUS_TEXT[$this->siteStatus],
            'siteStatus'             => $this->siteStatus,
            'siteAddress'            => $this->siteAddress,
            'placeholderTime'        => empty($this->placeholderTime) ? '' : date('Y-m-d H:i:s',
                $this->placeholderTime),
            'nearbyRange'            => bccomp(($this->nearbyRange ?? 0), '0.00') == 0 ? '' : ($this->nearbyRange ?? 0),
            'nearbyPlaceHolderRange' => bccomp(($this->nearbyPlaceHolderRange ?? 0), '0.00') == 0 ? '' : ($this->nearbyPlaceHolderRange ?? 0),
            'nearbyShopName'         => empty($this->nearbyShopCode) ? '该城市内无夸父门店' : (ShopService::getShopNameByCode($this->nearbyShopCode) ?? '该城市内无夸父门店'),
            'nearbyPlaceHolderName'  => $nearbyPlaceHolderName ?: '该城市内无预占点位',
            'isPlaceholder'          => (int)OpenShopSignOrderModel::query()->where('orderNo', $this->orderNo)->value('isPlaceholder'),
        ];
    }
}
